package jp.co.showbiz.admin.report.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import jp.co.showbiz.admin.report.imp.DigestContentImageDto;
import jp.co.showbiz.admin.report.imp.DigestDto;
import jp.co.showbiz.admin.report.imp.ReportDetailDto;
import jp.co.showbiz.admin.report.imp.ReportDto;
import jp.co.showbiz.admin.report.imp.ReportServiceImpl;
import jp.co.showbiz.admin.talent.imp.TalentDto;
import jp.co.showbiz.common.base.BaseActionSupport;
import jp.co.showbiz.common.base.BaseException;
import jp.co.showbiz.common.entity.CommonMstBean;
import jp.co.showbiz.common.util.StringUtil;

public class DigestAction extends BaseActionSupport {
	private static final long serialVersionUID = 733380373104982990L;

	private ReportDto rBean;
	private DigestDto dBean;
	private ReportDetailDto detail;
	private String disc;
	private String discValue;
	private List<TalentDto> talents;
	protected HashMap<String, String> globeMap;
	
	public DigestAction() throws BaseException, IOException, SQLException {
		globeMap = new HashMap<String, String>();
		globeMap.put("globe.path.commonImg", getText("globe.path.commonImg"));
		globeMap.put("globe.path.root", getText("globe.path.root"));
		globeMap.put("golbe.path.tempDigestImgPath", getText("golbe.path.tempDigestImgPath"));
		globeMap.put("globe.path.base", getText("globe.path.base"));
		globeMap.put("globe.path.photo", getText("globe.path.photo"));
		globeMap.put("globe.path.movie", getText("globe.path.movie"));
		globeMap.put("globe.path.flash", getText("globe.path.flash"));
		globeMap.put("globe.path.voice", getText("globe.path.voice"));
		
		globeMap.put("file.vga.thumb", getText("file.vga.thumb"));
		globeMap.put("file.qvga.thumb", getText("file.qvga.thumb"));
		globeMap.put("file.vga.middle", getText("file.vga.middle"));
		globeMap.put("file.qvga.middle", getText("file.qvga.middle"));
		globeMap.put("file.vga.complete", getText("file.vga.complete"));
		globeMap.put("file.qvga.complete", getText("file.qvga.complete"));
		globeMap.put("file.vga.complete2", getText("file.vga.complete2"));
		globeMap.put("file.qvga.complete2", getText("file.qvga.complete2"));
		
		globeMap.put("file.name.photo", getText("file.name.photo"));
		globeMap.put("file.name.shortmovie", getText("file.name.shortmovie"));
		globeMap.put("file.name.videoclip", getText("file.name.videoclip"));
		globeMap.put("file.name.flashcalender", getText("file.name.flashcalender"));
		globeMap.put("file.name.flashwait", getText("file.name.flashwait"));
		globeMap.put("file.name.voice", getText("file.name.voice"));
		
		ReportServiceImpl service = new ReportServiceImpl(globeMap);
		List<CommonMstBean> discs = service.getKijiDiscValue();
		for (CommonMstBean bean : discs) {
			globeMap.put(bean.getValue(), bean.getName());
		}
	}
	
	public String getDisc() {
		return disc;
	}

	public void setDisc(String disc) {
		this.disc = disc;
	}

	public String getDiscValue() {
		return discValue;
	}

	public void setDiscValue(String discValue) {
		this.discValue = discValue;
	}

	public DigestDto getDBean() {
		return dBean;
	}

	public void setDBean(DigestDto bean) {
		dBean = bean;
	}
	
	public ReportDto getRBean() {
		return rBean;
	}

	public void setRBean(ReportDto bean) {
		rBean = bean;
	}

	public ReportDetailDto getDetail() {
		return detail;
	}

	public void setDetail(ReportDetailDto detail) {
		this.detail = detail;
	}

	private void setTalentList() throws Exception {
		ReportServiceImpl service = new ReportServiceImpl(globeMap);
		talents = service.getTalentList();
	}
	
	public List<TalentDto> getTalents() {
		return talents;
	}

	public void setTalents(List<TalentDto> talents) {
		this.talents = talents;
	}

	@Override
	public void prepare() throws Exception {
		if (disc == null) disc = "";
		if (rBean == null) rBean = new ReportDto();
		if (dBean == null) dBean = new DigestDto();
		if (detail == null) detail = new ReportDetailDto();
	}
	
	private void setDiscValue() {
		if (disc.equals("cover")) {
			discValue = globeMap.get("co");
		} else if (disc.equals("interview")) {
			discValue = globeMap.get("in");
		} else if (disc.equals("issue")) {
			discValue = globeMap.get("is");
		} else if (disc.equals("wideshow")) {
			discValue = globeMap.get("wi");
		}
	}

    public String digestCreate() throws Exception {
    	logger.debug("ReportAction.digestCreate called.");
    	setDiscValue();
		setTalentList();
	    dBean.setModifyFlg(false);
        return SUCCESS;
	}
    public String digestRegiste() throws Exception {
    	logger.debug("ReportAction.digestRegiste called.");
    	setDiscValue();
    	
	    ReportServiceImpl service = new ReportServiceImpl(globeMap);
	    dBean.setKijiId(Integer.parseInt(rBean.getId()));
	    HashMap<String, Integer> point = service.getContentPoint();
	    service.registeDigest(dBean, rBean, point.get("PH"));
	    
    	List<DigestContentImageDto> dContents = service.getUploadedFileList();
    	int digestIndex = service.selectDigestMaxSeq(dBean.getId());
    	dContents = service.parseContentImgTag(dBean, dContents, digestIndex);
    	service.moveDigestContents(rBean, dBean, dContents, digestIndex, point.get("PH"));
    	service.updateDigestContents(dBean);
    	service.clearTempDigestImg();
    	return SUCCESS;
    }
    public String digestDetail() throws Exception {
    	logger.debug("ReportAction.digestDetail called.");
    	setDiscValue();
    	if (detail.getId() == null) {
    		detail.setId(rBean.getId());
    	}
	    ReportServiceImpl service = new ReportServiceImpl(globeMap);
	    dBean = service.getDigestDetail(dBean.getId(), false);
	    dBean.setContents(StringUtil.removeImgsize(dBean.getContents()));
    	return SUCCESS;
    }
    public String digestModify() throws Exception {
    	logger.debug("ReportAction.digestModify called.");
    	setDiscValue();
    	setTalentList();
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	dBean = service.getDigestDetail(dBean.getId(), true);
    	dBean.setModifyFlg(true);
    	return SUCCESS;
    }
    public String digestUpdate() throws Exception {
    	logger.debug("ReportAction.digestUpdate called.");
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	HashMap<String, Integer> point = service.getContentPoint();
    	service.updateDigest(dBean, rBean, point.get("PH"));
    	service.clearTempDigestImg();
    	return SUCCESS;
    }
}
